/*
 * @Author: 可以清心
 * @Description: 
 * @Date: 2022-10-08 14:50:18
 * @LastEditTime: 2023-05-19 14:51:35
 */
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const args = process.argv.slice(2);
const fileName = args && args.length && args[0];

/**
 * @description: zip文件夹
 * @param {string} sourceFolder 待压缩文件夹
 * @param {string} distZip 压缩后的zip文件
 * @param {function} cb 回调函数
 * @param {boolean} subdir 是否需要包一层
 * @return {void}
 */
function zipFolder(sourceFolder, distZip, cb, subdir){
    let output = fs.createWriteStream(distZip);

    let archive = archiver('zip', {
        zlib: {level: 9}
    });

    output.on('close', function(){
        cb(null, 'zip folder success!');
    })

    archive.on('error', function(err){
        cb(err);
    })

    archive.pipe(output);
    archive.directory(sourceFolder, subdir || false);
    archive.finalize()
}

function startZip(){
    let sourceFolder = path.resolve(__dirname, 'dist');
    let distZip = fileName + '.zip';

    zipFolder(sourceFolder, distZip, function(err, msg){
        if(err){
            console.error(err);
        }else{
            console.log(msg);
        }
    })
}

startZip();

